package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    TeacherDao teacherDao = new TeacherDao();

    //判断输入ID是否存在于数组中
    public boolean isExists(String id) {
        //接收数组中的老师对象数组
        Teacher[] teachers = teacherDao.findAllStudent();
        boolean exists = false;
        for (int i = 0; i < teachers.length; i++) {
            //拿到每一个老师对象
            Teacher teacher = teachers[i];
            //判断数组中的元素是否为空，元素ID是否与输入ID相符
            if (teacher != null && teacher.getId().equals(id)){
                exists = true;
            }
        }
        return exists;
    }

    //添加老师方法
    public boolean addTeacher(Teacher teacher, String id) {
        return teacherDao.addTeacher(teacher,id);
    }

    //查找老师方法
    public Teacher[] findAllTeacher() {
        //调用底层方法，拿到老师对象数组
        Teacher[] teachers = teacherDao.findAllStudent();
        boolean flag = false;

        //判断数组中是否存在元素
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null){
                flag = true;
                break;
            }
        }

        //存在元素则返回数组，不存在元素返回null
        if (flag) {
            return teachers;
        }else {
            return null;
        }
    }

    //删除老师方法
    public void deleteTeacherById(String deleteId) {
        teacherDao.deleteTeacherById(deleteId);
    }

    //修改老师方法
    public void updataTeacher(Teacher teacher, String updataId) {
        teacherDao.updataTeacher(teacher, updataId);
    }
}
